package mil.af.amc.adpetracker.model;

import org.hibernate.annotations.CollectionOfElements;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */

@Entity
@Table(name = "T_ORGANIZATION")
public class Organization implements Serializable, Comparable<Organization> {

    private int id;
    private String name;
    private Organization parent;
    private List<Organization> children;
    private List<User> assignedUsers;

    @Id
    @Column(name = "ORGANIZATIONID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ORGID_SEQ")
    @SequenceGenerator(name = "ORGID_SEQ", sequenceName = "S_ORGANIZATIONID")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    @JoinColumn(name = "PARENTID")
    public Organization getParent() {
        return parent;
    }

    public void setParent(Organization parent) {
        this.parent = parent;
    }

    //@OneToMany(mappedBy = "parent")
    @CollectionOfElements
    public List<Organization> getChildren() {
        return children;
    }

    public void setChildren(List<Organization> children) {
        this.children = children;
    }

    @OneToMany(mappedBy = "assignedOrganization")
    public List<User> getAssignedUsers() {
        return assignedUsers;
    }

    public void setAssignedUsers(List<User> assignedUsers) {
        this.assignedUsers = assignedUsers;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final Organization that = (Organization) o;

        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        if (name.trim().equalsIgnoreCase(that.getName().trim())) {
            return true;
        }

        return true;
    }

    public int hashCode() {
        return (name != null ? name.hashCode() : 0);
    }

    public int compareTo(Organization o) {
        return name.trim().compareToIgnoreCase(o.getName().trim());
    }
}
